#!/bin/bash
set -e
if [ $1 = "install" ] ; then
	if [ -e /restore-he.tar ] ; then
		mkdir -p /var/mobile/.ivrit/
		mv /restore-he.tar /var/mobile/.ivrit/restore-he.tar
	fi
	if [ ! -e /var/mobile/.ivrit/restore-he.tar ] ; then
		machine=`uname -m`
		iph2g=iPhone1,1
		ipd1g=iPod1,1
		ipd2g=iPod1,2
		iph3g=iPhone1,2

		#creating dirs
		echo "Preparing to make backup..."
		if [ $machine = $iph2g ] || [ $machine = $iph3g ] ; then
			mkdir -p /var/mobile/.ivrit/restore/Applications/MobilePhone.app/
			mkdir -p /var/mobile/.ivrit/restore/System/Library/Carrier\ Bundles/Unknown.bundle/
		fi
		mkdir -p /var/mobile/.ivrit/restore/Applications/MobileSafari.app/
		mkdir -p /var/mobile/.ivrit/restore/Applications/Preferences.app/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/CoreServices/SpringBoard.app/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/Fonts/Cache/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/Frameworks/AddressBook.framework/English.lproj/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/Frameworks/CoreLocation.framework/Support/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/Frameworks/UIKit.framework/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/Frameworks/UIKit.framework/PhoneFormats/
		mkdir -p /var/mobile/.ivrit/restore/System/Library/PrivateFrameworks/AppSupport.framework/
		mkdir -p /var/mobile/.ivrit/restore/private/var/mobile/Library/Preferences/

		#copying the files
		echo "Making backup... it can take a while"
		if [ $machine = $iph2g ] || [ $machine = $iph3g ] ; then
			cp -p /Applications/MobilePhone.app/EmergencyCallStrings.strings /var/mobile/.ivrit/restore/Applications/MobilePhone.app/
			cp -p /Applications/MobilePhone.app/MobilePhonePackedImages.artwork /var/mobile/.ivrit/restore/Applications/MobilePhone.app/
			cp -p /System/Library/Carrier\ Bundles/Unknown.bundle/carrier.plist /var/mobile/.ivrit/restore/System/Library/Carrier\ Bundles/Unknown.bundle/
		fi

		cp -p /Applications/MobileSafari.app/GoogleTLDs.plist /var/mobile/.ivrit/restore/Applications/MobileSafari.app/
		cp -p /Applications/Preferences.app/GlobalPreferences.plist /var/mobile/.ivrit/restore/Applications/Preferences.app/
		cp -p /System/Library/CoreServices/SpringBoard.app/EmergencyCallStrings.strings /var/mobile/.ivrit/restore/System/Library/CoreServices/SpringBoard.app/
		cp -p /System/Library/Fonts/Cache/Helvetica.ttf /var/mobile/.ivrit/restore/System/Library/Fonts/Cache/
		cp -p /System/Library/Fonts/Cache/HelveticaBold.ttf /var/mobile/.ivrit/restore/System/Library/Fonts/Cache/
		cp -p /System/Library/Frameworks/AddressBook.framework/ABAddressFormats.plist /var/mobile/.ivrit/restore/System/Library/Frameworks/AddressBook.framework/
		cp -p /System/Library/Frameworks/AddressBook.framework/English.lproj/ABContactSections.plist /var/mobile/.ivrit/restore/System/Library/Frameworks/AddressBook.framework/English.lproj/
		cp -p /System/Library/Frameworks/CoreLocation.framework/Support/timezones.plist /var/mobile/.ivrit/restore/System/Library/Frameworks/CoreLocation.framework/Support/
		cp -p /System/Library/Frameworks/UIKit.framework/Keyboard-default.plist /var/mobile/.ivrit/restore/System/Library/Frameworks/UIKit.framework/
		cp -p /System/Library/Frameworks/UIKit.framework/PhoneFormats/UIDefaultFormats.phoneformat /var/mobile/.ivrit/restore/System/Library/Frameworks/UIKit.framework/PhoneFormats/
		cp -p /System/Library/PrivateFrameworks/AppSupport.framework/all_cities_adj.plist /var/mobile/.ivrit/restore/System/Library/PrivateFrameworks/AppSupport.framework/
		cp -p /System/Library/PrivateFrameworks/AppSupport.framework/Default.phoneformat /var/mobile/.ivrit/restore/System/Library/PrivateFrameworks/AppSupport.framework/
		cp -p /System/Library/PrivateFrameworks/AppSupport.framework/DefaultCities.plist /var/mobile/.ivrit/restore/System/Library/PrivateFrameworks/AppSupport.framework/
		cp -p /System/Library/PrivateFrameworks/AppSupport.framework/PhoneNumberTemplates.plist /var/mobile/.ivrit/restore/System/Library/PrivateFrameworks/AppSupport.framework/
		cp -p /System/Library/PrivateFrameworks/AppSupport.framework/timezoneToCity.plist /var/mobile/.ivrit/restore/System/Library/PrivateFrameworks/AppSupport.framework/
		cp -pR /private/var/mobile/Library/Preferences/ /var/mobile/.ivrit/restore/private/var/mobile/Library/
		
		cd /var/mobile/.ivrit/restore/
		tar cpf /var/mobile/.ivrit/restore-he.tar *
		cd /var/mobile/.ivrit/
		rm -drf restore/
	fi
	iKeyEx-KBMan add HebKeyboard
	exit 0
fi

